package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    //显示列表
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, String name) {
        //调用service
        Page<Dish> dishList = dishService.findByPage(pageNum, pageSize, name);
        //返回
        return ResultInfo.success(dishList);
    }

    //添加数据
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        //diaoyong
        dishService.save(dish);
        return ResultInfo.success();
    }

    //回显数据
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        //调用service
        Dish dish = dishService.findByid(id);
        return ResultInfo.success(dish);
    }

    //修改菜品
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        //调用service
        dishService.update(dish);
        //返回
        return ResultInfo.success();
    }

    //将菜品状态
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
        //调用service
        dishService.updateStatus(status, ids);
        return ResultInfo.success();
    }

    //删除菜品
    @DeleteMapping("/dish")
    public ResultInfo deleteBatch(@RequestParam List<Long> ids) {
        dishService.deleteBatch(ids);
        return ResultInfo.success();
    }

    //根据菜品id查询菜品
    @GetMapping("/dish/list")
    public ResultInfo findListBySetmeal(Long categoryId, String name) {
        //调用service
        List<Dish> dishList = dishService.findListBySetmeal(categoryId, name);
        return ResultInfo.success(dishList);
    }


}
